import {Injectable} from '@angular/core';
import {
  CanActivate, // 守卫，处理导航到某路由的情况。
  ActivatedRouteSnapshot,
  RouterStateSnapshot, Router
} from '@angular/router';
import {environment} from '../../environments/environment';
import {CommonTool} from '../common/common.tool';
import {Token} from './base.model';
import {NzMessageService} from 'ng-zorro-antd';

@Injectable()
export class BaseRouteFilter implements CanActivate {
  constructor(public commonTool: CommonTool,
              private router: Router) {
  }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    const token = this.commonTool.getCookie(environment.systemToken);
    if (this.commonTool.notNull(token)) {
      const tokenModel: Token = JSON.parse(sessionStorage.getItem(environment.systemToken));
      const date = tokenModel.createTime.replace(/-/g, '/');
      if ((new Date(date).getTime() + tokenModel.expiryTime) > new Date().getTime()) {
        return true;
      }
    }
    this.router.navigate(['/login']);
    return false;
  }


}
